package com.wzh.project.utils.conflictJar;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.wzh.project.common.bean.ConfictJarClass;
import com.wzh.project.utils.PrimaryUUID;

/**
 * 
 * 输出冲突的jar包和类名
 * 
 * @author wzh
 *
 */
public class PrintfConflictJarAndClass {

	private static final Logger logger = LogManager.getLogger(PrintfConflictJarAndClass.class);

	/**
	 * 所有的内容
	 */
	private static Map<String, String> class_package_jarName = new HashMap<String, String>();
	/**
	 * 存放产生冲突的类和jar包
	 */
	private static Map<String, ConfictJarClass> confictClass = new HashMap<String, ConfictJarClass>();

		
	private static String outFilePath="D:"+File.separatorChar+"conflictJar-"+PrimaryUUID.getUUID()+".txt";
	/**
	 * 主方法
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		logger.debug("start=========");
		String filePath="D:\\Users\\wzh\\test\\wzh-fwzx-devtest\\lib";
		inputAllJarFile(filePath);
		printfOutFile();
		PrintfConflictJarAndClass test=new PrintfConflictJarAndClass();
		test.inputAllJarFile(filePath);
		test.printfOutFile();
		logger.debug("end=========");
	}
	
	/**
	 * 处理该路径下所有的jar包筛选出其中冲突的类和jar名
	 * @param filePath
	 * @throws Exception 
	 */
	public static void dealFilePath(String filePath) throws Exception {
		inputAllJarFile(filePath);
		printfOutFile();
	}

	/**
	 * 读取指定路径下的jar包文件
	 * 
	 * @param filePath 路径
	 * @throws Exception
	 */
	public static void inputAllJarFile(String filePath) throws Exception {
		File root = new File(filePath);
		File[] file = root.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				if (name.endsWith(".jar")) {
					return true;
				}
				return false;
			}
		});
		if (file==null||file.length<1) {
			logger.debug("该路径下没有符合该规则的文件");
			return ;
		}
		for (File file2 : file) {
			String fullFilePath=file2.getPath();
			logger.debug("现在扫描的文件名为"+fullFilePath);
			JarFile jar = new JarFile(file2);
			String jarName=fullFilePath.substring(fullFilePath.lastIndexOf(File.separatorChar)+1);
			Enumeration<JarEntry> enumFiles = jar.entries();
			JarEntry entry;
			while (enumFiles.hasMoreElements()) {
				entry = (JarEntry) enumFiles.nextElement();
				if (entry.getName().indexOf("META-INF") < 0) {
					String classFullName = entry.getName();
					if (!classFullName.endsWith(".class")) {
						classFullName = classFullName.substring(0, classFullName.length() - 1);
					} else {
						String className = classFullName.replace("/", ".");
						if (class_package_jarName.get(className)==null) {
							class_package_jarName.put(className, jarName);
						}else {
							if (confictClass.get(className)==null) {
								ConfictJarClass claJarClass=new ConfictJarClass();
								claJarClass.setClassName(className);
								List<String> jarNameList=new ArrayList<String>();
								jarNameList.add(jarName);
								jarNameList.add(class_package_jarName.get(className));
								claJarClass.setJarName(jarNameList);
								confictClass.put(className, claJarClass);
							}else {
								confictClass.get(className).getJarName().add(jarName);
							}
						}
					}
				}
			}
		}
	}

	/**
	 * 比较该类名是否存在
	 * 
	 * @param jarName
	 * @param packageName
	 * @param className
	 * @throws Exception
	 */
	public static void compare(String jarName, String packageName, String className) throws Exception {

	}

	/**
	 * 输出到文件中冲突的jar包
	 * 
	 * @throws Exception
	 */
	public static void printfOutFile() throws Exception {
		if (confictClass.isEmpty()) {
			logger.debug("该路径下没有产生冲突的文件");
			return;
		}
		logger.debug("生成的文件路径:"+outFilePath);
		File file=new File(outFilePath);
		OutputStream outputStream=new FileOutputStream(file, true);
		BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(outputStream);
		for (Entry<String, ConfictJarClass> entry : confictClass.entrySet()) {
			StringBuffer stringBuffer=new StringBuffer();
			ConfictJarClass confictJarClass=entry.getValue();
			stringBuffer.append(confictJarClass.getClassName()+":\r\n\t\t\t\t");
			for (String jarName : confictJarClass.getJarName()) {
				stringBuffer.append(jarName);
				stringBuffer.append("\r\n\t\t\t\t");
			}
			stringBuffer.append("\r\n");
			bufferedOutputStream.write(stringBuffer.toString().getBytes());
        }
		bufferedOutputStream.flush();
		bufferedOutputStream.close();
		
	}

}
